package com.hotel_management_backstage.lin.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Collections;

public class JwtUser implements UserDetails{

    private Integer id;

    private String name;

    private String password;

    private Collection<? extends GrantedAuthority> authorities;

    public JwtUser(String name, String password, Collection<? extends GrantedAuthority> authorities) {
        this.name = name;
        this.password = password;
        this.authorities = authorities;
    }

    //写一个可以直接使用SysUser创建jwtUser的构造器
    public JwtUser(SysUser sysUser){
        this.id = sysUser.getId();
        this.name = sysUser.getName();
        this.password = sysUser.getPassword();
        //这里只存储了一个角色的名字
        this.authorities = Collections.singleton(new SimpleGrantedAuthority(sysUser.getRole()));
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return name;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
